package cn.veliger.page;

import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

/**
 * @author veliger@163.com
 * @create 2018-07-02 19:49
 * @desc
 **/
@Getter
@Setter
public class PageResult<T> {
    private List rows;
    private Integer total;
    private Integer currentPage;
    private Integer pageSize;
    private Integer totalPage;
    private Integer prePage;
    private Integer nextPage;
    public Integer getTotalPage() {
        return totalPage == 0 ? 1 : totalPage;
    }

    /**
     * pageHelper使用
     * @param rows
     */
    public PageResult(List<T> rows) {
        PageInfo<T> pageInfo = new PageInfo<>(rows);
        this.rows = pageInfo.getList();
        this.total = pageInfo.getSize();
        this.currentPage = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        totalPage = pageInfo.getPages();
        prePage = pageInfo.getPrePage();
        nextPage = pageInfo.getNextPage();
    }

    /**
     * 空结果
     * @param pageSize
     * @return
     */
    public static PageResult emptyPage(Integer pageSize) {
        return new PageResult(Collections.emptyList(), 0, 1, pageSize);
    }

    /**
     * 普通分页使用
     * @param rows
     * @param total
     * @param currentPage
     * @param pageSize
     */
    public PageResult(List rows, Integer total, Integer currentPage, Integer pageSize) {
        super();
        this.rows = rows;
        this.total = total;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        prePage = currentPage - 1 >= 1 ? currentPage - 1 : 1;
        nextPage = currentPage + 1 <= totalPage ? currentPage + 1 : totalPage;
    }
}
